<!--
 * @Author: [kangjie]
 * @Date: 2022-04-20 14:58:13
 * @LastEditors: 45654 2275061717@qq.com
 * @LastEditTime: 2022-10-17 15:51:34
 * @Description:
-->
<template>
  <div>
    <!-- 新增按钮区域 -->
    <el-button type="success" @click="toAdd">新增</el-button>
    <!-- 新增按钮区域结束 -->

    <!-- 表格区域 -->
    <el-table
      :data="baseConfigs"
      style="width: 100%"
      :header-cell-style="{ 'text-align': 'center' }"
      :cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column type="index" :index="1" label="序号" width="60" />
      <el-table-column prop="name" label="名称" />
      <el-table-column prop="val" label="值">
        <template slot-scope="scope">
          <span v-if="scope.row.val.indexOf('http://') === -1">{{
            scope.row.val
          }}</span>
          <img v-else :src="scope.row.val" alt="" />
        </template>
      </el-table-column>
      <el-table-column prop="introduce" label="介绍" />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <a href="#" class="toEdit" @click.prevent="toEdit(scope.row)">编辑</a>
          <a href="#" class="toDelete" @click.prevent="toDelete(scope.row.id)"
            >删除</a
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格区域结束 -->

    <!-- 模态框区域 -->
    <el-dialog :title="title" :visible.sync="visible" @close="toClose">
      <el-form ref="form" :rules="rules" :model="form">
        <el-form-item label="名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" autocomplete="off" />
        </el-form-item>
        <el-form-item
          label="介绍"
          :label-width="formLabelWidth"
          prop="introduce"
        >
          <el-input v-model="form.introduce" autocomplete="off" />
        </el-form-item>
        <el-form-item label="类型" :label-width="formLabelWidth">
          <el-select v-model="type" placeholder="请选择活动区域">
            <el-option label="文字" value="文字" />
            <el-option label="图片" value="图片" />
          </el-select>
        </el-form-item>
        <el-form-item
          v-if="type === '文字'"
          label="值"
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.val" autocomplete="off" />
        </el-form-item>
        <!-- 上传图片区域 -->
        <el-form-item v-else label="图片" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            :action="uploadFileURL"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="form.val" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <!-- 上传图片区域结束 -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="toClose">取 消</el-button>
        <el-button type="primary" @click="toSubmit()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 模态框区域结束 -->
  </div>
</template>
<script>
import { get, post } from "@/utils/request";
import { uploadFileURL } from "@/utils/config";
export default {
  data() {
    return {
      // 所有配置信息变量
      baseConfigs: [],
      // 模态框标题
      title: "",
      // 表单数据
      form: {},
      formLabelWidth: "120px",
      // 模态框显示与隐藏
      visible: false,
      // 上传图片变量
      imageUrl: "",
      // 图片上传地址
      uploadFileURL,
      // 选择框默认文字
      type: "文字",
      // 表单验证规则
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        introduce: [{ required: true, message: "请输入介绍", trigger: "blur" }],
      },
    };
  },
  watch: {
    type() {
      this.form.val = "";
    },
  },
  created() {
    this.getAllbaseConfig();
  },
  methods: {
    toSubmit() {
      // 校验表单
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          // 发送保存或者更新请求
          const res = await post("/baseConfig/saveOrUpdate", this.form);
          this.$notify({
            type: "success",
            message: res.message,
          });
          this.getAllbaseConfig();
          this.toClose();
          this.form = {};
        } else {
          return false;
        }
      });
    },
    // 关闭模态框回调
    toClose() {
      // 重置表单
      setTimeout(() => {
        this.form = {};
        this.$refs.form.resetFields();
        this.visible = false;
      }, 500);
    },
    // 图片上传
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.val = res.data;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    // 编辑配置信息事件
    toEdit(row) {
      this.form = { ...row };
      this.title = "编辑配置信息";
      this.visible = true;
      this.imageUrl = "123";
    },
    // 删除配置信息事件
    toDelete(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await get("/baseConfig/deleteById", { id });
          this.$message({
            type: "success",
            message: res.message,
          });
          this.getAllbaseConfig();
          this.toClose();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 获取所有配置信息
    async getAllbaseConfig() {
      const res = await get("/baseConfig/findAll");
      this.baseConfigs = res.data;
      // console.log(res.data);
    },
    // 新增配置信息
    toAdd() {
      this.visible = true;
      this.title = "新增基础配置";
    },
  },
};
</script>
<style lang="scss">
.toEdit {
  color: #30b08f;
}
.toDelete {
  color: #e65d6e;
}

// 图片上传样式
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

// 图片大小
img {
  width: 60px;
}
</style>
